package com.shenzhenai.it_hei_ma.a12deadlock;

/**
 * @author huangzehui
 * @date 29/8/2025 上午12:03 周五
 * @description
 */
public class MyThread extends Thread {

    static final Object objA = new Object();
    static final Object objB = new Object();


    @Override
    public void run() {
        while (true) {
            if ("线程A".equals(getName())) {
                synchronized (objA) {
                    System.out.println(getName() + "拿到了A锁，准备拿B锁");
                    synchronized (objB) {
                        System.out.println(getName() + "拿到了B锁，顺利执行完1轮");
                    }
                }
            } else if ("线程B".equals(getName())) {
                synchronized (objB) {
                    System.out.println(getName() + "拿到了B锁，准备拿A锁");
                    synchronized (objA) {
                        System.out.println(getName() + "拿到了A锁，顺利执行完1轮");
                    }
                }
            }
        }
    }
}
